﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.IO;

namespace DisplayBoardInfo.Advertisement
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class Advertisement : UserControl
    {
        string[] images;
        int imagesCount;
        int currentFrame = 0;

        public Advertisement()
        {
            InitializeComponent();
            
            InitImages();

            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(2);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void InitImages()
        {
            try
            {
                var mainDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
                var imagesDirectory = new DirectoryInfo(System.IO.Path.Combine(mainDirectory, "images"));
                images = imagesDirectory.GetFiles().Select(file => "images/" + file.Name).ToArray();
                imagesCount = images.Count();
            }
            //design mode
            catch (Exception)
            {

            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (imagesCount > 0)
            {
                currentFrame = (currentFrame + 1) % imagesCount;
                image.Source = new BitmapImage(new Uri(images[currentFrame], UriKind.Relative));
            }
        }
    }
}
